unit options;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, IniFiles, fgl;

const
  MainSection: String = 'App';

type

  TKeyNVal = specialize TFPGMap<string, string>;

  { TOptions }

  TOptions = class
  private
    FFilename: string;
    FIniFile: TIniFile;
  protected
    FValues: TKeyNVal;
  public
    constructor Create(AFileName: string);
    destructor Destroy;
    function getAppPath: string;
    function getInt(const paramName: string; valDefault: integer): integer;
    function getString(const paramName: string; valDefault: string): string;
    procedure setInt(const paramName: string; value: integer);
    procedure setString(const paramName: string; value: string);
    function Save:boolean;
    procedure Reset;virtual;
  end;

implementation

{ TOptions }

constructor TOptions.Create(AFileName: string);
begin
  FFilename := AFileName;
  FIniFile := TIniFile.Create(FFilename);
end;

destructor TOptions.Destroy;
begin
  FreeAndNil(FIniFile);
end;

function TOptions.getAppPath: string;
var
  AppPath: string;
begin
  AppPath := ExtractFilePath(ParamStr(0));
  Result := IncludeTrailingBackslash(AppPath);
end;

function TOptions.getInt(const paramName: string; valDefault: integer): integer;
begin
  FIniFile.readInteger(MainSection, paramName, valDefault);
end;

function TOptions.getString(const paramName: string; valDefault: string): string;
begin
  FIniFile.readString(MainSection, paramName, valDefault);
end;

procedure TOptions.setInt(const paramName: string; value: integer);
begin
  setString(paramName, IntToStr(value));
end;

procedure TOptions.setString(const paramName: string; value: string);
begin
  FValues.KeyData[paramName]:= value;
end;

function TOptions.Save: boolean;
begin
  while FValues.Count > 0 do
  begin
    FIniFile.WriteString(MainSection, FValues.Keys[0], FValues.Data[0]);
    FValues.Delete(0);
  end;
end;

procedure TOptions.Reset;
begin
  // NOP
end;

end.


